package com.xinzhiduming.mp3palyer;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FolderListViewAdapter extends BaseAdapter{

	private LayoutInflater mLayoutInflater = null;
	Boolean isEmptyFolder = false;
	ArrayList<File> mFolders = new ArrayList<File>();
	
	public FolderListViewAdapter(ArrayList<File> theFileList, Context context) {
		if (theFileList == null) {
			isEmptyFolder = true;
		}else {
			mFolders = theFileList;
		}
		mLayoutInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return 0;
		}
		return mFolders.size();
	}

	@Override
	public File getItem(int position) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return null;
		}
		return mFolders.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return 0;
		}
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (isEmptyFolder) {
			return null;
		}
		ViewHoler viewHoler;
		if (convertView == null) {
			viewHoler = new ViewHoler();
			convertView = mLayoutInflater.inflate(R.layout.select_folder_listview_detail, null);
			viewHoler.FileFodlerTv = (TextView) convertView.findViewById(R.id.file_or_folder_name);
			viewHoler.FileFolderIv = (ImageView) convertView.findViewById(R.id.icon_view);
			convertView.setTag(viewHoler);
		} else {
			viewHoler = (ViewHoler) convertView.getTag();
		}

		if (!getItem(position).isDirectory()) {
			if (MediaFile.isAudioFileType(getItem(position).toString())) {
				viewHoler.FileFolderIv.setImageResource(R.drawable.file);
				viewHoler.FileFodlerTv.setTextColor(android.graphics.Color.GREEN);
				viewHoler.FileFodlerTv.setTypeface(null, android.graphics.Typeface.BOLD);			
			}else {
				viewHoler.FileFolderIv.setImageResource(R.drawable.file);
				viewHoler.FileFodlerTv.setTextColor(android.graphics.Color.DKGRAY);
				viewHoler.FileFodlerTv.setTypeface(android.graphics.Typeface.DEFAULT);
			}
		} else {
			viewHoler.FileFolderIv.setImageResource(R.drawable.folder);
			viewHoler.FileFodlerTv.setTextColor(android.graphics.Color.YELLOW);
			viewHoler.FileFodlerTv.setTypeface(null, android.graphics.Typeface.BOLD_ITALIC);
		}
		viewHoler.FileFodlerTv.setText(getItem(position).getName());

		return convertView;
	}
	
	public static class ViewHoler {
		public ImageView FileFolderIv;
		public TextView FileFodlerTv;
	}
}
